package com.softeem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softeem.domain.User;
import com.softeem.mapper.UserMapper;
import com.softeem.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-07-01
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl service;

    @RequestMapping("/login")
    public String login(@ModelAttribute("uId")String uId,
                        @ModelAttribute("uPassword")String uPassword,
                        HttpServletRequest req,
                        HttpServletResponse resp,
                        //@CookieValue("username")String username,
                        Model model){
        QueryWrapper<User> qw = new QueryWrapper<>();

        User user =  new User();
        qw.eq("uId",uId);
        qw.eq("uPassword",uPassword);
        List<User> users = user.selectList(qw);


        if (users.size()==0){
            System.out.println("用户登录失败");
            return "/login";
        }else {
            System.out.println("用户登录成功");
            System.out.println(users);
            Cookie cookie = new Cookie("username",users.get(0).getuName());
            Cookie c2 = new Cookie("uId",uId);

            cookie.setPath("/");
            c2.setPath("/");
            cookie.setMaxAge(60*10);//10分钟
            resp.addCookie(cookie);
            resp.addCookie(c2);

            Cookie[] cookies = req.getCookies();
            for (Cookie cookie1 : cookies) {
                if (cookie1.getName().equals("username")){
                    resp.addCookie(cookie1);
                }
                if (c2.getName().equals("uId")){
                    resp.addCookie(c2);
                }
            }


            //model.addAttribute("username",username);

            return "/indexUser";
        }


    }

    @RequestMapping("/userRegister")
    public String userRegister(@ModelAttribute("uAccount")String uAccount,
                               @ModelAttribute("uName")String uName,
                               @ModelAttribute("uPassword")String uPassword,
                               @ModelAttribute("uMale")String uMale,
                               @ModelAttribute("uEmail")String uEmail,
                               @ModelAttribute("uIntroduce")String uIntroduce){

        User user = new User();
        user.setuAccount(uAccount);
        user.setuName(uName);
        user.setuPassword(uPassword);
        user.setuMale(uMale);
        user.setuEmail(uEmail);
        user.setuIntroduce(uIntroduce);


        List<User> list = service.addUser(user);
        for (User user1 : list) {
            System.out.println(user1);
        }
        System.out.println("添加成功");

        return "/indexUser";
     }

}

